                            # 动手试一试
# 10-1 # 统计数字1在pi前一百万位中出现的概率
filename = 'test_files/pi_million_digits.txt'

with open(filename) as file_object:
    lines = file_object.readlines()

pi_string = ''

numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
cnt = []
for idx in range(0, 10):
    cnt.append(0)

for line in lines:
    pi_string += line.strip()

for number in pi_string:
    if number in numbers:
        cnt[int(number)] += 1

pr_num = []
for item in cnt:
    pr = item / len(pi_string)
    pr_num.append(pr)

print(pr_num)
# 10-2
# 10-3 访客
filename = 'test_files/guest.txt'

user_name = ''
with open(filename, 'w') as file_object:
    while user_name != 'quit':
        user_name = input("Please enter a user's name(enter 'quit' to quit): ")
        if user_name != 'quit':
            file_object.write(user_name.title() + "\n")
        else:
            print("Thank you!")
            break
# 10-4 访客名单
# 10-5
# 10-6 加法运算
while True:
    first_number = input("\nFirst number: ")
    if first_number == 'q':
        break
    second_number = input("\nSecond number: ")
    if second_number == 'q':
        break
    try:
        answer = int(first_number) + int(second_number)
    except ValueError:
        print("Illegal input!")
    else:
        print(answer)
# 10-7
# 10-8 猫
cats_name = 'test_files/cats.txt'
with open(cats_name, 'w') as f_obj:
    for i in range(0, 3):
        name = input("Please enter a cat's name: ")
        f_obj.write(name + "\n")
# 10-9
# 10-10 常见单词
filename = 'test_files/siddhartha.txt'

count = 0
try:
    with open(filename) as f_obj:
        contents = f_obj.read()
except FileNotFoundError:
    msg = "Sorry, the file '" + filename + "' does not exist."
    print(msg)
else:
    for word in contents:
        if 'the' in contents:
            count += 1
    print("单词'the'出现了" + str(count) + "次")

# 10-11
# 10-12 最喜欢的数字
import json

def get_favorite_number():
    """如果存储了喜欢的数字，就获取它"""
    filename = 'test_files/favorite_number.json'
    try:
        with open(filename) as f_obj:
            favorite_number = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return  favorite_number

def get_new_number():
    """提示用户输入喜欢的数字，存储并获取之"""
    msg = "What is your favorite number? "
    favorite_number = input(msg)

    filename = 'test_files/favorite_number.json'
    with open(filename, 'w') as f_obj:
        json.dump(favorite_number, f_obj)

    return favorite_number

def show_favorite_number():
    """显示用户最喜欢的数字"""
    favorite_number = get_favorite_number()
    if favorite_number != None:
        print("I know your favorite number!It's " + favorite_number)
    else:
        favorite_number = get_new_number()

show_favorite_number()
# 10-13 验证用户
import json

def get_stored_username():
    """如果存储了用户名，就获取它"""
    filename = 'test_files/username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    """提示用户输入用户名，存储并获取之"""
    username = input("What is your name? ")

    filename = 'test_files/username.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)

    return username

def greet_users():
    """问候用户，并指出其名字"""
    username = get_stored_username()
    if username != None:
        ask = "current username: " + username
        ask += ("\nIs the username correct?(yes/ no): ")
        response = input(ask)
        if response == 'no':
            username = get_new_username()
            print("We'll remember you when you come back, " + username + "!")
        else:
            print("Welcome back, " + username + "!")
    else:
        username = get_new_username()
        print("We'll remember you when you come back, " + username + "!")

greet_users()